import common.ListNode;

/**
 * @author tmh
 * @date 2024/9/20 16:14
 * @description
 */
public class T2两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(-1);
        ListNode cur = result;
        int count = 0;
        while (l1 != null || l2 != null || count != 0) {
            int sum = 0;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            sum += count;
            count = sum >= 10 ? sum / 10 : 0;
            result.next = new ListNode(sum % 10);
            result = result.next;
        }
        return cur.next;
    }

}
